<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="58px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="专业组" >
          <el-select v-model="queryParams.laboratoryProGroupId" placeholder="请选择专业组" clearable >
            <el-option v-for="item in professionalList" :key="item.id" :label="item.name"
              :value="item.id" />
          </el-select>
        </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" clearable style="width: 240px">
          <el-option value="" label="全部"></el-option>
          <el-option v-for="item in statusType" :key="item.status" :label="item.title" :value="item.status" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:dict:add']">
          新增对照
        </el-button>

      </el-col>
      <el-col :span="1.5">
        <el-dropdown trigger="click">
          <el-button type="primary" size="mini">
            导出<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleExport('now')">导出当前</el-dropdown-item>
            <el-dropdown-item @click.native="handleExport('all')">导出全部</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
    </el-row>
    <el-table border :row-class-name="rowStyle" v-loading="loading" :data="projectTypeList"
      @selection-change="handleSelectionAllChange" height="calc(100vh - 250px)" :header-cell-style="{
        background: '#ebebeb'
      }">
      <af-table-column type="selection" width="55" align="center" />
      <af-table-column label="序号" align="center" type="index" />
      <af-table-column label="编码" align="center" prop="code" />
      <af-table-column label="标准码" align="center" width="120" prop="standardCode" />
      <af-table-column label="名称" align="center" width="350" prop="name" />
      <af-table-column label="检验方法" align="center" prop="testMethod" />
      <af-table-column label="实验室专业组" align="center" prop="laboratoryProGroupName" />
      <af-table-column label="分管原则" align="center" prop="inChargePrinciple" />
      <!-- <af-table-column label="标本类型" align="center" prop="specimenType" /> -->
      <af-table-column label="组合/套餐" align="center" prop="comboOrPackage" />
      <af-table-column label="前缀" align="center" prop="prefix" />
      <af-table-column label="流程号" align="center" prop="numPrefix" />
      <af-table-column label="报告时间" align="center" prop="reportTime">
        <template slot-scope="{row}">{{ row.reportTime }}个工作日</template>
      </af-table-column>
      <af-table-column label="备注" align="center" prop="remark" />
      <af-table-column label="是否自动分析" align="center" prop="status" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isAnalyze == 1" active-color="#169bd5" inactive-color="#aaaaaa"
            @change="switchShowAnalyStatus(scope.row)" />
        </template>
      </af-table-column>
      <af-table-column label="是否自动分析备注" align="center" prop="analyzeRemark" />
      
      <el-table-column label="状态" align="center" prop="status" fixed="right" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status == 1" active-color="#169bd5" inactive-color="#aaaaaa"
            @change="switchShowStatus(scope.row)" />
        </template>
      </el-table-column>


      <el-table-column label="操作" fixed="right" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <pagination style="margin:0;padding: 0;" :total="total" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog title="获取LRP数据" :visible.sync="lrpOpen" width="1400px" append-to-body>

      <!-- 记得改 -->
      <el-form ref="lrpForm" :model="lrpForm" :rules="rules" label-width="" style="display: flex;" :inline="true">
        <!-- <el-form-item label="状态" prop="status">
          <el-select v-model="lrpForm.status" placeholder="全部" clearable size="small">
            <el-option v-for="item in statusType" :key="item.status" :label="item.title" :value="item.status" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="专业组" prop="labDeptId">
          <el-select v-model="lrpForm.labDeptId" placeholder="请选择专业组" clearable size="small">
            <el-option v-for="item in professionalList" :key="item.labDeptId" :label="item.name"
              :value="item.labDeptId" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="keyword">
          <el-input v-model="lrpForm.keyword" placeholder="请输入项目名称" />
        </el-form-item>
        
        <el-form-item>
          <el-button size="mini" style="margin-left: 20px; height: 36px;" type="primary" @click="getLrpList">获取LRP数据
          </el-button>
        </el-form-item>
      </el-form>

      <div style="display: flex;">
        <!-- :data="lrpList.slice((currentPage1 - 1) * pageSize, currentPage1 * pageSize)" -->
        <el-table ref="multipleTable" border v-loading="loading"
          :data="lrpList"
          @selection-change="handleSelectionChange"
          height="500">
          <el-table-column type="selection" width="55" align="center" :selectable="setDisabled" />
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="编码" align="center" prop="codeNo" show-overflow-tooltip />
          <el-table-column label="标准码" align="center" prop="standCodeNo" show-overflow-tooltip />
          <el-table-column label="名称" align="center" prop="name" show-overflow-tooltip />
          <el-table-column label="检验方法" align="center" prop="sampleTestMethodName" show-overflow-tooltip />
          <el-table-column label="实验室专业组" align="center" prop="labDeptName" width="100" show-overflow-tooltip />
          <el-table-column label="标本类型" align="center" prop="specimenType" show-overflow-tooltip />
          <el-table-column label="报告时间" align="center" prop="experimentTimeDay" show-overflow-tooltip />
        </el-table>

        <div class="transferBtn" @click="choose">
          <img src="../../../assets/image/right.png" style="width: 50px;height: 50px;" alt="">
        </div>

        <el-table :row-class-name="tableRowClassName" border v-loading="loading" :data="projectCopyList"  height="500">
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="编码" align="center" prop="code" show-overflow-tooltip />
          <el-table-column label="标准码" align="center" prop="standardCode" show-overflow-tooltip />
          <el-table-column label="名称" align="center" prop="name" show-overflow-tooltip />
          <el-table-column label="前缀" align="center" prop="prefix" show-overflow-tooltip />
          <el-table-column label="检验方法" align="center" prop="testMethod" show-overflow-tooltip />
          <el-table-column label="实验室专业组" align="center" prop="laboratoryProGroupName" width="100"
            show-overflow-tooltip />
          <el-table-column label="标本类型" align="center" prop="specimenType" show-overflow-tooltip />
          <el-table-column label="报告时间" align="center" prop="experimentTimeDay" show-overflow-tooltip />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button v-if="scope.row.codeNo" size="mini" type="text" icon="el-icon-delete"
                @click="handleDelete(scope.row)" style="color: #FA5555;" v-hasPermi="['system:dict:edit']">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        

      </div>
      <!-- <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="currentPage1" :page-sizes="[10, 20]" :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper" :total="totalList" style="margin-top:10px">
        </el-pagination> -->

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="refer">提 交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="项目类型：编辑" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" :disabled="true" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" :disabled="true" />
        </el-form-item>
        <el-form-item label="前缀" prop="prefix">
          <el-input v-model="form.prefix" placeholder="请输入前缀"></el-input>
        </el-form-item>
        <el-form-item label="流程号" prop="numPrefix">
          <el-input v-model="form.numPrefix" placeholder="请输入流程号"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
        <el-form-item label="自动分析备注" prop="analyzeRemark">
          <el-input v-model="form.analyzeRemark" placeholder="请输入自动分析备注"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import { projectType, projectTypeEdit, projectTypeUpdate, lrpProjectList, projectBatchComparison, projectTypeExport } from "@/api/dictionary/systemDictionary/projectType"
import { professional } from "@/api/dictionary/systemDictionary/professional"

import { sampleType } from '@/api/dictionary/systemDictionary/sampleType'

export default {
  name: "projectType",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 项目类型数据
      projectTypeList: [],
      projectCopyList: [],
      submitList: [],
      lrpList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      lrpOpen: false,
      statusType: [
        {
          title: '开启',
          status: 1
        },
        {
          title: '禁用',
          status: 0
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: undefined,
        status: '',
      },
      // 表单参数
      form: {},
      //LRP数据
      lrpForm: {
        pageSize:10000,
        labDeptId:'176408377170216279'
      },
      // 表单校验
      rules: {

      },
      professionalList: [],
      currentPage1: 1,
      pageSize: 10,
      totalList:0
    }
  },
  created() {
    this.getList()
    professional().then(res => {
        this.professionalList = res.rows
      })
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      console.log(val);
      //一页显示多少条
      this.pageSize = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      console.log(val);
      //默认第一页，当你点击就把点击的那页赋值给currentPage1
      this.currentPage1 = val
    },

    /** 查询项目类型列表 */
    getList() {
      this.loading = true
      projectType(this.queryParams).then(response => {
        this.projectTypeList = response.rows
        this.total = response.total
        this.loading = false
      }
      )
    },
    // 取消按钮
    cancel() {
      this.projectCopyList = []
      this.submitList = []
      this.lrpList = []
      this.lrpOpen = false
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.queryParams.laboratoryProGroupId=''
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      
      this.reset()
      this.lrpOpen = true
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.submitList = []
      selection.map(item => {
        item.id = ''
        item.laboratoryProGroup = item.labDeptId
        item.code = item.codeNo
        item.standardCode = item.standCodeNo
        //记得改
        // item.name = 'ackhask'
        item.comboOrPackage = item.itemType
        // item.name = item.sourceName
        item.testMethod = item.sampleTestMethodName
        item.laboratoryProGroupName = item.labDeptName
        item.reportTime = item.experimentTimeDay
        let dataFind = this.projectCopyList.find(zhe => zhe.code === item.code)
        if (dataFind) {
          item.id = dataFind.id
          item.prefix = dataFind.prefix
        }
        this.submitList.push(item)
      })
      console.log(this.submitList);
    },
    //选中的数据
    choose() {
      for (let k = 0; k < this.projectCopyList.length; k++) {
        let assignment = this.submitList.find(
          item => item.codeNo === this.projectCopyList[k].code
        )
        if (assignment) {
          this.projectCopyList[k] = assignment
          this.submitList.splice(
            this.submitList.findIndex(
              ge => ge.code === this.projectCopyList[k].code
            ),
            1
          )
        }
      }
      this.projectCopyList = [...this.projectCopyList, ...this.submitList]
      this.clearSelection()
    },
    //清除勾选
    clearSelection() {
      this.$refs.multipleTable.clearSelection()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      projectTypeEdit(id).then(response => {
        console.log(response);
        this.form = response.data
        this.open = true
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            projectTypeUpdate(this.form).then(response => {
              this.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除选中的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(res => {
        console.log(row)
        this.projectCopyList.splice(
          this.projectCopyList.findIndex(
            item => item.codeNo === row.codeNo
          ),
          1
        )
      })
    },
    // /** 导出按钮操作 */
    handleExport(e) {
      var different = ''
      var params = ''
      if (e === 'now') {
        different = '当前'
        params = this.queryParams //对应接口参数
      } else if (e === 'all') {
        different = '所有'
        params = null
      } else {
        return
      }
      let that = this
      let loading
      this.$confirm('是否确认导出' + different + '数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        loading = that.$loading({
          lock: true,
          text: '导出中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        projectType(params).then((response) => {
          let mydata = [
            [
              '序号',
              '编码',
              '标准码',
              '名称',
              '检验方法',
              '实验室专业组',
              '分管原则',
              '标本类型',
              '组合/套餐',
              '前缀',
              '报告时间',
              '备注',
              '是否自动分析备注',
              '是否自动分析',
              '状态',
            ],
            ...response.rows.map((item, index) => ([
              index + 1,
              item.code,
              item.standardCode,
              item.name,
              item.testMethod,
              item.laboratoryProGroupName,
              item.inChargePrinciple,
              item.specimenType,
              item.comboOrPackage,
              item.prefix,
              item.reportTime + '个工作日',
              item.remark,
              item.analyzeRemark,
              item.isAnalyze === 0 ? '禁用' : '开启',
              item.status === 0 ? '禁用' : '开启',
            ]))
          ]
          let requireMerge = []
          that.$util.createxlsx({
            mydata,
            fileName: '项目类型',
            dataList: response.rows,
            type: 'orderId',
            requireMerge,
            loading
          })
        })
      }).catch(function () { })
    },
    //切换状态
    switchShowStatus(row) {
      if (row.status === 1) {
        this.handleUnPublish(row)
      } else if (row.status === 0) {
        this.handlePublish(row)
      }
    },
    handlePublish(row) {
      this.$confirm('是否开启', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return projectTypeUpdate({
          ...row,
          status: 1
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },
    handleUnPublish(row) {
      this.$confirm('是否禁用', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return projectTypeUpdate({
          ...row,
          status: 0
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },

    //自动分析
    switchShowAnalyStatus(row) {
      if (row.isAnalyze === 1) {
        this.handleAnalyzeUnPublish(row)
      } else if (row.isAnalyze === 0) {
        this.handleAnalyzePublish(row)
      }
    },
    handleAnalyzePublish(row) {
      this.$confirm('是否开启自动分析', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return projectTypeUpdate({
          ...row,
          isAnalyze: 1
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },
    handleAnalyzeUnPublish(row) {
      this.$confirm('是否禁用自动分析', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return projectTypeUpdate({
          ...row,
          isAnalyze: 0
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },



    handleSelectionAllChange(selection) {
      this.ids = selection.map(item => item.id)
    },


    //获取lrp数据
    getLrpList() {
      this.loading=true
      lrpProjectList(this.lrpForm).then(response => {
        this.lrpList = response.data.records
        // this.totalList=response.data.records.length
        this.loading=false
        console.log(response)
      })
      projectType().then(response => {
        this.projectCopyList = response.rows
      }
      )
    },

    //选择数据后禁用选择框
    setDisabled(row, index) {
      let dataFind = this.projectCopyList.find(item => item.code === row.codeNo)
      if (dataFind) {
        if ((row.codeNo + row.standCodeNo + row.sourceName + row.sampleTestMethodName + row.labDeptName) === (dataFind.code + dataFind.standardCode + dataFind.name + dataFind.testMethod + dataFind.laboratoryProGroupName)) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    //新增和数据修改改变颜色
    tableRowClassName({ row, rowIndex }) {
      let dataFind = this.projectCopyList.find(item => item.code === row.codeNo)
      if (dataFind) {
        if ((row.codeNo + row.standCodeNo + row.sourceName + row.sampleTestMethodName + row.labDeptName) === (dataFind.code + dataFind.standardCode + dataFind.name + dataFind.testMethod + dataFind.laboratoryProGroupName)) {
          return 'success-row'
        } else {
          return ''
        }
      } else {
        return ''
      }
    },
    //提交新增对照
    refer() {
      // console.log(this.projectCopyList,this.projectCopyList)
      let delLenth = this.projectCopyList.filter(item => {
        if (!item.codeNo && !item.standCodeNo) {
          return true
        }
      })
      for (let i = 0; i < delLenth.length; i++) {
        this.projectCopyList.splice(
          this.projectCopyList.findIndex(
            item => {
              if (!item.codeNo && !item.standCodeNo) {
                return true
              } else {
                return false
              }
            }
          ),
          1
        )
      }
      if (!this.projectCopyList.length) {
        this.msgError("请获取lrp数据")
        return
      }
      projectBatchComparison({ dictProjectTypeList: this.projectCopyList }).then(response => {
        this.msgSuccess("提交成功")
        this.lrpOpen = false
        this.projectCopyList = []
        this.submitList = []
        this.lrpList = []
        this.getList()
      })
    },
    rowStyle({ row }) {
      var arr = this.ids
      for (let i = 0; i < arr.length; i++) {
        if (row.id === arr[i]) {
          return "rowStyle"
        }
      }
    },
  }
}
</script>
<style scoped>
.app-container {
  height: 85vh;
}

.transferBtn {
  padding: 0 10px 0 10px;
  cursor: pointer;
  /* margin-top: 50%; */
  display: flex;
  align-items: center;
}

::v-deep .success-row {
  background: rgba(47, 120, 236, 0.19) !important;
}

::v-deep .rowStyle {
  background-color: #a8d6fe !important;
}

::v-deep .el-table__body tr:hover>td {
  background: #a8d6fe !important;
}

::v-deep .el-table__body tr.current-row>td {

  background: #a8d6fe !important;
}
::v-deep .el-dialog__body{
  padding:0 20px !important
}
</style>
